package com.caicai.demo;
import java.io.FileReader;
import java.util.ArrayList;
import org.json.simple.JSONArray;
import org.json.simple.parser.JSONParser;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class CoordinateChecker {

    @RequestMapping("/one")
    public void one(){
        try {
            // 读取 JSON 文件
            String root = System.getProperty("user.dir");
            //System.out.println(root);
            String FileName= "latlon.json";
            JSONArray coordinates = (JSONArray) new JSONParser().parse(new FileReader(FileName));

            // 定义经纬度范围的四个角点坐标
            double p1_lng = 119.037089;
            double p1_lat = 32.259867;
            double p2_lng = 118.821489;
            double p2_lat = 32.077388;
            double p3_lng = 118.80657099;
            double p3_lat = 32.0353893;
            double p4_lng = 118.894173;
            double p4_lat = 32.077862;

            // 存储经纬度范围的四个角点坐标
            ArrayList<double[]> boundingBox = new ArrayList<double[]>();
            boundingBox.add(new double[] {p1_lng, p1_lat});
            boundingBox.add(new double[] {p2_lng, p2_lat});
            boundingBox.add(new double[] {p3_lng, p3_lat});
            boundingBox.add(new double[] {p4_lng, p4_lat});

            // 判断每个点是否在经纬度范围内
            for (Object coord : coordinates) {
                double lng = (double) ((JSONArray) coord).get(0);
                double lat = (double) ((JSONArray) coord).get(1);
                if (isPointInBoundingBox(lng, lat, boundingBox)) {
                    System.out.println("点 (" + lng + ", " + lat + ") 在范围里");
                } else {
                    System.out.println("点 (" + lng + ", " + lat + ") 不在范围里");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 判断点是否在经纬度范围内
    private static boolean isPointInBoundingBox(double lng, double lat, ArrayList<double[]> boundingBox) {
        double maxLng = boundingBox.get(0)[0];
        double minLng = boundingBox.get(0)[0];
        double maxLat = boundingBox.get(0)[1];
        double minLat = boundingBox.get(0)[1];

        for (double[] point : boundingBox) {
            if (point[0] > maxLng) maxLng = point[0];
            if (point[0] < minLng) minLng = point[0];
            if (point[1] > maxLat) maxLat = point[1];
            if (point[1] < minLat) minLat = point[1];
        }

        if (lng <= maxLng && lng >= minLng && lat <= maxLat && lat >= minLat) {
            return true;
        } else {
            return false;
        }
    }
}


